
<template>
  <div class="asset-query">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>资产信息查询</span>
        </div>
      </template>

      <!-- 查询方式选择 -->
      <el-radio-group v-model="queryType" class="query-type-group">
        <el-radio-button label="qr">扫码查询</el-radio-button>
        <el-radio-button label="employee">员工资产查询</el-radio-button>
        <el-radio-button label="advanced">高级查询</el-radio-button>
      </el-radio-group>

      <!-- 扫码查询 -->
      <div v-if="queryType === 'qr'" class="query-section">
        <el-form :inline="true" class="qr-form">
          <el-form-item label="资产编码/二维码">
            <el-input v-model="qrCode" placeholder="请输入资产编码或扫描二维码" clearable style="width: 300px">
              <template #append>
                <el-button icon="Scan" @click="handleScanQR">扫码</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQRQuery">查询</el-button>
          </el-form-item>
        </el-form>

        <!-- 资产详情展示 -->
        <div v-if="assetDetail" class="asset-detail">
          <el-descriptions title="资产详细信息" :column="2" border>
            <el-descriptions-item label="资产编码">{{ assetDetail.code }}</el-descriptions-item>
            <el-descriptions-item label="资产名称">{{ assetDetail.name }}</el-descriptions-item>
            <el-descriptions-item label="资产分类">
              <el-tag :type="getCategoryType(assetDetail.category)">
                {{ getCategoryLabel(assetDetail.category) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="规格型号">{{ assetDetail.model }}</el-descriptions-item>
            <el-descriptions-item label="使用部门">{{ assetDetail.department }}</el-descriptions-item>
            <el-descriptions-item label="使用人">{{ assetDetail.user }}</el-descriptions-item>
            <el-descriptions-item label="存放位置">{{ assetDetail.location }}</el-descriptions-item>
            <el-descriptions-item label="资产状态">
              <el-tag :type="getStatusType(assetDetail.status)">
                {{ getStatusLabel(assetDetail.status) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="购置日期">{{ assetDetail.purchaseDate }}</el-descriptions-item>
            <el-descriptions-item label="资产价值">¥{{ assetDetail.value.toLocaleString() }}</el-descriptions-item>
            <el-descriptions-item label="用友编号" v-if="assetDetail.category === 'fixed'">{{ assetDetail.yyCode || '未关联' }}</el-descriptions-item>
            <el-descriptions-item label="现状描述" :span="2">{{ assetDetail.description }}</el-descriptions-item>
          </el-descriptions>

          <!-- 资产图片 -->
          <div class="asset-images" v-if="assetDetail.images && assetDetail.images.length">
            <h4>资产图片</h4>
            <el-image
              v-for="(image, index) in assetDetail.images"
              :key="index"
              :src="image"
              :preview-src-list="assetDetail.images"
              fit="cover"
              class="asset-image"
            />
          </div>
        </div>
      </div>

      <!-- 员工资产查询 -->
      <div v-if="queryType === 'employee'" class="query-section">
        <el-form :inline="true" :model="employeeSearchForm" class="search-form">
          <el-form-item label="员工姓名">
            <el-select
              v-model="employeeSearchForm.employeeId"
              placeholder="请选择员工"
              filterable
              remote
              reserve-keyword
              :remote-method="searchEmployees"
              :loading="employeeLoading"
              style="width: 300px"
            >
              <el-option
                v-for="item in employeeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleEmployeeQuery">查询</el-button>
          </el-form-item>
        </el-form>

        <!-- 员工资产列表 -->
        <el-table v-if="employeeAssets.length" :data="employeeAssets" border style="width: 100%">
          <el-table-column prop="code" label="资产编码" width="120" />
          <el-table-column prop="name" label="资产名称" width="180" />
          <el-table-column prop="category" label="资产分类" width="120">
            <template #default="scope">
              <el-tag :type="getCategoryType(scope.row.category)">
                {{ getCategoryLabel(scope.row.category) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="model" label="规格型号" width="150" />
          <el-table-column prop="location" label="存放位置" width="150" />
          <el-table-column prop="status" label="资产状态" width="100">
            <template #default="scope">
              <el-tag :type="getStatusType(scope.row.status)">
                {{ getStatusLabel(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="purchaseDate" label="购置日期" width="120" />
          <el-table-column prop="value" label="资产价值" width="120">
            <template #default="scope">
              ¥{{ scope.row.value.toLocaleString() }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="scope">
              <el-button link type="primary" @click="handleViewAsset(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 高级查询 -->
      <div v-if="queryType === 'advanced'" class="query-section">
        <el-form :model="advancedSearchForm" label-width="120px" class="advanced-form">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="资产分类">
                <el-select v-model="advancedSearchForm.category" placeholder="请选择资产分类" clearable style="width: 100%">
                  <el-option label="固定资产" value="fixed" />
                  <el-option label="低值易耗品" value="low_value" />
                  <el-option label="租赁资产" value="lease" />
                  <el-option label="租入固定资产改良" value="lease_improvement" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="使用部门">
                <el-select v-model="advancedSearchForm.department" placeholder="请选择使用部门" clearable style="width: 100%">
                  <el-option label="财务部" value="finance" />
                  <el-option label="行政部" value="admin" />
                  <el-option label="技术部" value="tech" />
                  <el-option label="市场部" value="marketing" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="资产状态">
                <el-select v-model="advancedSearchForm.status" placeholder="请选择资产状态" clearable style="width: 100%">
                  <el-option label="在用" value="in_use" />
                  <el-option label="闲置" value="idle" />
                  <el-option label="维修中" value="maintenance" />
                  <el-option label="已报废" value="scrapped" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="购置日期范围">
                <el-date-picker
                  v-model="advancedSearchForm.purchaseDateRange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="价值范围">
                <el-input-number
                  v-model="advancedSearchForm.minValue"
                  placeholder="最小值"
                  :min="0"
                  style="width: 45%"
                />
                <span style="margin: 0 5px">-</span>
                <el-input-number
                  v-model="advancedSearchForm.maxValue"
                  placeholder="最大值"
                  :min="0"
                  style="width: 45%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关键字">
                <el-input v-model="advancedSearchForm.keyword" placeholder="资产名称/编码/规格" clearable />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="handleAdvancedQuery">查询</el-button>
            <el-button @click="resetAdvancedSearch">重置</el-button>
            <el-button type="success" @click="handleExport">导出查询结果</el-button>
          </el-form-item>
        </el-form>

        <!-- 高级查询结果 -->
        <el-table v-if="advancedResults.length" :data="advancedResults" border style="width: 100%">
          <el-table-column prop="code" label="资产编码" width="120" />
          <el-table-column prop="name" label="资产名称" width="180" />
          <el-table-column prop="category" label="资产分类" width="120">
            <template #default="scope">
              <el-tag :type="getCategoryType(scope.row.category)">
                {{ getCategoryLabel(scope.row.category) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="model" label="规格型号" width="150" />
          <el-table-column prop="department" label="使用部门" width="120" />
          <el-table-column prop="user" label="使用人" width="120" />
          <el-table-column prop="location" label="存放位置" width="150" />
          <el-table-column prop="status" label="资产状态" width="100">
            <template #default="scope">
              <el-tag :type="getStatusType(scope.row.status)">
                {{ getStatusLabel(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="purchaseDate" label="购置日期" width="120" />
          <el-table-column prop="value" label="资产价值" width="120">
            <template #default="scope">
              ¥{{ scope.row.value.toLocaleString() }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="scope">
              <el-button link type="primary" @click="handleViewAsset(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div v-if="advancedResults.length" class="pagination-container">
          <el-pagination
            v-model:current-page="advancedPagination.currentPage"
            v-model:page-size="advancedPagination.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="advancedPagination.total"
            @size-change="handleAdvancedSizeChange"
            @current-change="handleAdvancedCurrentChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 查询方式
const queryType = ref('qr')

// 扫码查询相关
const qrCode = ref('')
const assetDetail = ref(null)

// 员工资产查询相关
const employeeLoading = ref(false)
const employeeOptions = ref([])
const employeeSearchForm = reactive({
  employeeId: ''
})
const employeeAssets = ref([])

// 高级查询相关
const advancedSearchForm = reactive({
  category: '',
  department: '',
  status: '',
  purchaseDateRange: [],
  minValue: null,
  maxValue: null,
  keyword: ''
})
const advancedResults = ref([])
const advancedPagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 获取资产分类标签
const getCategoryLabel = (category) => {
  const categoryMap = {
    fixed: '固定资产',
    low_value: '低值易耗品',
    lease: '租赁资产',
    lease_improvement: '租入固定资产改良'
  }
  return categoryMap[category] || category
}

// 获取资产分类类型
const getCategoryType = (category) => {
  const typeMap = {
    fixed: '',
    low_value: 'success',
    lease: 'warning',
    lease_improvement: 'info'
  }
  return typeMap[category] || ''
}

// 获取资产状态标签
const getStatusLabel = (status) => {
  const statusMap = {
    in_use: '在用',
    idle: '闲置',
    maintenance: '维修中',
    scrapped: '已报废'
  }
  return statusMap[status] || status
}

// 获取资产状态类型
const getStatusType = (status) => {
  const typeMap = {
    in_use: 'success',
    idle: 'info',
    maintenance: 'warning',
    scrapped: 'danger'
  }
  return typeMap[status] || ''
}

// 扫码查询
const handleScanQR = () => {
  ElMessage.info('请使用手机扫描二维码')
  // 实际应该调用扫码功能
}

// 执行扫码查询
const handleQRQuery = () => {
  if (!qrCode.value) {
    ElMessage.warning('请输入资产编码或扫描二维码')
    return
  }

  // 模拟查询结果
  assetDetail.value = {
    id: 1,
    code: qrCode.value || 'ZC20230001',
    name: '笔记本电脑',
    category: 'fixed',
    model: 'ThinkPad X1 Carbon',
    department: '技术部',
    user: '张三',
    location: '总部A座501',
    status: 'in_use',
    purchaseDate: '2023-01-15',
    value: 8500,
    yyCode: 'YY20230001',
    description: '技术部开发用笔记本电脑，配置较高',
    images: [
      'https://picsum.photos/seed/laptop1/400/300.jpg',
      'https://picsum.photos/seed/laptop2/400/300.jpg'
    ]
  }

  ElMessage.success('查询成功')
}

// 搜索员工
const searchEmployees = (query) => {
  if (query) {
    employeeLoading.value = true
    // 模拟API调用
    setTimeout(() => {
      employeeOptions.value = [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
        { id: 3, name: '王五' },
        { id: 4, name: '赵六' }
      ].filter(item => item.name.includes(query))
      employeeLoading.value = false
    }, 500)
  } else {
    employeeOptions.value = []
  }
}

// 执行员工资产查询
const handleEmployeeQuery = () => {
  if (!employeeSearchForm.employeeId) {
    ElMessage.warning('请选择员工')
    return
  }

  // 模拟查询结果
  employeeAssets.value = [
    {
      id: 1,
      code: 'ZC20230001',
      name: '笔记本电脑',
      category: 'fixed',
      model: 'ThinkPad X1 Carbon',
      department: '技术部',
      user: '张三',
      location: '总部A座501',
      status: 'in_use',
      purchaseDate: '2023-01-15',
      value: 8500
    },
    {
      id: 2,
      code: 'ZC20230002',
      name: '显示器',
      category: 'fixed',
      model: 'Dell U2720Q',
      department: '技术部',
      user: '张三',
      location: '总部A座501',
      status: 'in_use',
      purchaseDate: '2023-02-20',
      value: 3200
    }
  ]

  ElMessage.success('查询成功')
}

// 执行高级查询
const handleAdvancedQuery = () => {
  // 模拟查询结果
  advancedResults.value = [
    {
      id: 1,
      code: 'ZC20230001',
      name: '笔记本电脑',
      category: 'fixed',
      model: 'ThinkPad X1 Carbon',
      department: '技术部',
      user: '张三',
      location: '总部A座501',
      status: 'in_use',
      purchaseDate: '2023-01-15',
      value: 8500
    },
    {
      id: 2,
      code: 'ZC20230002',
      name: '显示器',
      category: 'fixed',
      model: 'Dell U2720Q',
      department: '技术部',
      user: '张三',
      location: '总部A座501',
      status: 'in_use',
      purchaseDate: '2023-02-20',
      value: 3200
    },
    {
      id: 3,
      code: 'ZC20230003',
      name: '办公桌',
      category: 'low_value',
      model: '标准办公桌',
      department: '行政部',
      user: '李四',
      location: '总部B座201',
      status: 'in_use',
      purchaseDate: '2023-03-10',
      value: 1200
    }
  ]

  advancedPagination.total = 50
  ElMessage.success('查询成功')
}

// 重置高级查询
const resetAdvancedSearch = () => {
  Object.keys(advancedSearchForm).forEach(key => {
    if (key === 'minValue' || key === 'maxValue') {
      advancedSearchForm[key] = null
    } else if (key === 'purchaseDateRange') {
      advancedSearchForm[key] = []
    } else {
      advancedSearchForm[key] = ''
    }
  })
  advancedResults.value = []
  ElMessage.success('重置成功')
}

// 导出查询结果
const handleExport = () => {
  ElMessage.success('正在导出查询结果...')
  // 实际应该调用导出功能
}

// 查看资产详情
const handleViewAsset = (row) => {
  // 切换到扫码查询模式并显示资产详情
  queryType.value = 'qr'
  qrCode.value = row.code
  handleQRQuery()
}

// 高级查询分页大小改变
const handleAdvancedSizeChange = (size) => {
  advancedPagination.pageSize = size
  // 实际应该重新加载数据
}

// 高级查询当前页改变
const handleAdvancedCurrentChange = (page) => {
  advancedPagination.currentPage = page
  // 实际应该重新加载数据
}
</script>

<style scoped>
.asset-query {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.query-type-group {
  margin-bottom: 20px;
}

.query-section {
  margin-top: 20px;
}

.qr-form {
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.advanced-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.asset-detail {
  margin-top: 20px;
}

.asset-images {
  margin-top: 20px;
}

.asset-images h4 {
  margin-bottom: 10px;
}

.asset-image {
  width: 200px;
  height: 150px;
  margin-right: 10px;
  margin-bottom: 10px;
  border-radius: 4px;
  cursor: pointer;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
